﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using r4u.business.logic.layer.RECRUIT;
using r4u.data.access.layer;

namespace r4u.business.logic.layer.ASSESS
{
    public class Compare
    {
        public double recommandationIndex, experienceIndex, educationIndex, eligibleIndex, locationIndex, ageIndex, certificationIndex, projectIndex, skillIndex, total;
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string PictureURL { get; set; }

        public Compare(VACANCY vacancy, Candidate candidate)
        {

            #region Basic Data
            this.FirstName = candidate.FirstName;
            this.LastName = candidate.LastName;
            this.PictureURL = candidate.PictureURL;
            #endregion

            #region Recommandations
            this.recommandationIndex = 0;
            if (candidate.NoOfRecommandations >= 10)
            {
                this.recommandationIndex = 4;
            }
            else if (candidate.NoOfRecommandations >= 5)
            {
                this.recommandationIndex = 3;
            }
            else if (candidate.NoOfRecommandations >= 3)
            {
                this.recommandationIndex = 2;
            }
            else if (candidate.NoOfRecommandations >= 1)
            {
                this.recommandationIndex = 1;
            }
            else
            {
                this.recommandationIndex = 0;
            }
            #endregion

            #region Experience
            this.experienceIndex = 0;
            if (candidate.Experience >= 10)
            {
                this.experienceIndex = 4;
            }
            else if (candidate.Experience >= 5)
            {
                this.experienceIndex = 3;
            }
            else if (candidate.Experience >= 3)
            {
                this.experienceIndex = 2;
            }
            else if (candidate.Experience >= 1)
            {
                this.experienceIndex = 1;
            }
            else
            {
                this.experienceIndex = 0;
            }
            #endregion

            #region Education
            this.educationIndex = 0;

            if (candidate.EducationLevels.Contains(EducationLevel.PHD)
                || candidate.EducationLevels.Contains(EducationLevel.MD)
                || candidate.EducationLevels.Contains(EducationLevel.JD))
            {
                this.educationIndex = 4;
            }
            else if (candidate.EducationLevels.Contains(EducationLevel.Msc)
                || candidate.EducationLevels.Contains(EducationLevel.MBA)
                || candidate.EducationLevels.Contains(EducationLevel.EngineersDegree))
            {
                this.educationIndex = 3;
            }
            else if (candidate.EducationLevels.Contains(EducationLevel.Bsc)
                || candidate.EducationLevels.Contains(EducationLevel.AssociateDegree))
            {
                this.educationIndex = 2;
            }
            else if (candidate.EducationLevels.Contains(EducationLevel.HighSchool)
                || candidate.EducationLevels.Contains(EducationLevel.Other))
            {
                this.educationIndex = 1;
            }
            else
            {
                this.educationIndex = 0;
            }
            #endregion

            #region Eligible
            this.eligibleIndex = 0;

            if (candidate.CheckQualify(vacancy.vacancy_id))
            {
                this.eligibleIndex = 4;
            }
            else
            {
                this.eligibleIndex = 0;
            }
            #endregion

            #region Location
            this.locationIndex = 0;

            if (!string.IsNullOrEmpty(vacancy.location_of_applicant))
            {
                if (candidate.CountryCode.Equals(vacancy.location_of_applicant))
                {
                    this.locationIndex = 4;
                }
            }
            #endregion

            #region Age
            this.ageIndex = 0;

            if (Enumerable.Range(20, 29).Contains(candidate.Age))
            {
                this.ageIndex = 4;
            }
            else if (Enumerable.Range(30, 39).Contains(candidate.Age))
            {
                this.ageIndex = 3;
            }
            else if (Enumerable.Range(40, 44).Contains(candidate.Age))
            {
                this.ageIndex = 2;
            }
            else if (Enumerable.Range(45, 49).Contains(candidate.Age))
            {
                this.ageIndex = 1;
            }
            else
            {
                this.ageIndex = 0;
            }
            #endregion

            #region Certifications
            this.certificationIndex = 0;

            if (candidate.Certifications.Count >= 5)
            {
                this.certificationIndex = 4;
            }
            else if (candidate.Certifications.Count >= 3)
            {
                this.certificationIndex = 3;
            }
            else if (candidate.Certifications.Count >= 2)
            {
                this.certificationIndex = 2;
            }
            else if (candidate.Certifications.Count >= 1)
            {
                this.certificationIndex = 1;
            }
            else
            {
                this.certificationIndex = 0;
            }
            #endregion

            #region Projects
            this.projectIndex = 0;

            if (candidate.Projects.Count >= 5)
            {
                this.projectIndex = 4;
            }
            else if (candidate.Projects.Count >= 3)
            {
                this.projectIndex = 3;
            }
            else if (candidate.Projects.Count >= 2)
            {
                this.projectIndex = 2;
            }
            else if (candidate.Projects.Count >= 1)
            {
                this.projectIndex = 1;
            }
            else
            {
                this.projectIndex = 0;
            }
            #endregion

            #region Skills
            this.skillIndex = 0;

            foreach (VACANCY_SKILL skill in vacancy.VACANCY_SKILL)
            {
                if (string.IsNullOrEmpty(skill.optional) || skill.optional.Equals("N"))
                {
                    if (candidate.ContainsSkill(skill.skill_id))
                        this.skillIndex += 1;
                }
                else
                {
                    if (candidate.ContainsSkill(skill.skill_id))
                        this.skillIndex += 1;
                }
            }

            if (vacancy.VACANCY_SKILL.Count > 0)
                skillIndex = (skillIndex / vacancy.VACANCY_SKILL.Count) * 4;
            #endregion

            #region Total
            this.total = recommandationIndex + experienceIndex + educationIndex + eligibleIndex + locationIndex + ageIndex + certificationIndex + projectIndex + skillIndex;
            #endregion
        }
    }
}
